package com.lj.wordmark.modelresolve;

import com.lj.wordmark.utils.ReflectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @Author luojing
 * @Date 2022/4/17
 * 数据模型解析器
 */
public class ModelResolve {
    private static final Map<Class<?>, DataModelResolve> RESOLVES = new HashMap<>(16);

    static {
        RESOLVES.put(ModelResolve.class, new CommonModelResolve());
        RESOLVES.put(Date.class, new DateModelResolve());
        RESOLVES.put(LocalDate.class, new LocalDateModelResolve());
        RESOLVES.put(LocalDateTime.class, new LocalDateTimeModelResolve());
        RESOLVES.put(String.class, new StringDataModel());
    }

    /**
     * 通过数据模型类型获取对应的数据模型解析器
     *
     * @param o 数据模型的class对象
     * @return 数据模型解析器，可能为空
     */
    public static <T> DataModelResolve<T> getDataModelResolve(Class<T> o) {
        if (ReflectUtils.isBaseType(o)) {
            return RESOLVES.get(ModelResolve.class);
        }
        return RESOLVES.get(o);
    }
}
